/**
 * 
 */
package lt.ruta.data.table;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import lt.ruta.RutaMidlet;
import lt.ruta.data.Table;

/**
 * @author Vincentas
 *
 */
public class FileTableSender extends BaseSender {

	private String file;
	
	public FileTableSender(String file) {
		this.file = file;
	}
	
	
	/* (non-Javadoc)
	 * @see lt.ruta.data.table.BaseSender#send(lt.ruta.data.Table)
	 */
	public void send(Table table) {
		try {
			FileConnection fileConnection = (FileConnection) Connector.open("file:///" + getFile());
			try {
				if (!fileConnection.exists()) {
					fileConnection.create();
				} else {
					fileConnection.truncate(0);
				}
				
				table.writeTo(fileConnection.openOutputStream());
			} finally {
				fileConnection.close();
			}
		} catch (IOException exception) {
			RutaMidlet.instance.showError(exception);
		} catch (SecurityException securityException) {
			RutaMidlet.instance.showError(securityException);
		}
	}


	/**
	 * @return the file
	 */
	public String getFile() {
		return file;
	}


	/**
	 * @param file the file to set
	 */
	public void setFile(String file) {
		this.file = file;
	}
}
